package com.huadi.qi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huadi.qi.bean.Admin;
import com.huadi.qi.bean.User;
import com.huadi.qi.mapper.AdminMapper;
import com.huadi.qi.mapper.UserMapper;
import com.huadi.qi.service.AdminService;
import com.huadi.qi.service.UserService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;

    //登录
    @GetMapping("/login")
    public String login(){
        return "/admin/login";
    }
    // post 重定向
    @PostMapping("/login")
    public String post(String userId, String userPwd, HttpSession session, Model model){
        System.out.println(userId);
        System.out.println(userPwd);
        Admin admin = adminMapper.selectByName(String.valueOf(userId));
        try {
            if (admin.getName().equals(userId) && admin.getPwd().equals(userPwd)){
                session.setAttribute("loginAdmin",admin);
                return "redirect:/admin/index.html";
            }else {
                //登录失败，给出提示
                model.addAttribute("msg", "账号密码错误");
                //返回登录界面
                return "/admin/login";
            }
        }catch (Exception e){
            //登录失败，给出提示
            model.addAttribute("msg", "账号密码错误");
            //返回登录界面
            return "/admin/login";
        }
    }
    //  主页
    @GetMapping("/index.html")
    public String index(){
        return "/admin/index";
    }
    //在线答疑
    @GetMapping("/chat")
    public String chat(){
        return "/admin/chat";
    }

    //用户
    @GetMapping("/user")
    public String user(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                       @RequestParam(defaultValue = "") String name,Model model) {

            //分页数据查询
            Page<User> UserPage = new Page<>(pn, 4);
            //分页查询的结果
            Page<User> page = userService.page(UserPage, null);
            //总页数
            long pages = page.getPages();
            //每页显示多少条记录
            final long size = page.getSize();
            //当前页
            long current = page.getCurrent();
            //总记录数
            long total = page.getTotal();
            //遍历User的数据
            List<User> records = page.getRecords();
//        List<User> list = new ArrayList<>();
//        if (name.equals("")) {
//            list =userService.list();
//        }else {
//            list = userMapper.selectUser(name);
//      }
            model.addAttribute("user", page);
            return "/admin/user";
        }


    }
